﻿using GlennQuigmire.API.Application.Commands;
using IMES.Common.Domain.SirModel;
using IMES.Common.Dtos;
using MediatR;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace GlennQuigmire.API.Controllers
{
    /// <summary>
    /// 不良品项API--控制器
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class DefectiveController : ControllerBase
    {

        private readonly IMediator mediator;

        public DefectiveController(IMediator mediator)
        {
            this.mediator = mediator;
        }

        /// <summary>
        /// 添加不良品项
        /// </summary>
        /// <param name="cmd">添加不良品项请求对象</param>
        /// <returns>添加不良品项响应对象</returns>
        [HttpPost]
        public async Task<Result> DefectivesCreate(DefectiveCreateCommand cmd)
        {
            return await mediator.Send(cmd);
        }

        /// <summary>
        /// 查询显示不良品项信息
        /// </summary>
        /// <param name="cmd">查询不良品项信息请求对象</param>
        /// <returns>查询不良品项信息响应对象</returns>
        [HttpPost]
        public async Task<ResultPage<Defectives>> DefectivesSearch(DefectiveSearchCommand cmd)
        {
            return await mediator.Send(cmd);
        }

        /// <summary>
        /// 上传图片    
        /// </summary>
        /// <param name="file">图片文件</param>
        /// <returns>返回图片路径</returns>
        [HttpPost]
        public IActionResult UploadFile(IFormFile file)
        {
            string fileName  = file.FileName;
            fileName = DateTime.Now.ToString("yyyyMMddHHmmssfff") + "_" + Path.GetFileName(fileName).ToLower();















            //路径
            string path = Directory.GetCurrentDirectory()+@"\wwwroot\Images\"+fileName;
            //使用文件流完成文件上传
            using (var stream = new FileStream(path, FileMode.Create))
            {
                //将文件拷贝文件流中
                file.CopyTo(stream);
                //刷新缓冲区(冲洗文件流)
                stream.Flush();
            }
            string url = "/Images/"+fileName;
            return Ok(url);
        }



    }
}
